﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Catering.View_Operaction;
using Catering_Rules;
using System.Data;
using System.Text;

namespace Catering.Catering_Page.Kufang
{
    public partial class rjcbzs : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                LoadCheckBoxData();
                for (int i = 0; i < CheckBoxList1.Items.Count; i++)
                {
                    CheckBoxList1.Items[i].Selected = true;
                }
            }
        }

        private void LoadCheckBoxData()
        {
            T_RawMaterials rawMaterialsRule = new T_RawMaterials();
            List<Catering_Model.E_RawMaterials> listMaterials = rawMaterialsRule.GetAllList(0);
            Bind_Data_to_Controls.Bind_Material_cbo(listMaterials, this.CheckBoxList1);
        }

        protected void LinkButton1_Click(object sender, EventArgs e)
        {
            View_Monitor vm = new View_Monitor();
            int companyid = 0;
            int AreaID = Convert.ToInt32(this.DDLTool1.GetDDL_Area());
            int ClassID = Convert.ToInt32(this.DDLTool1.GetDDL_Class());
            int Year = 1949;
            if (!string.IsNullOrEmpty(this.DDLTool1.GetYear())) Year = Convert.ToInt32(this.DDLTool1.GetYear());
            int[] rawid = GetRaw();
            string[] rawName = GetRawName();
            if (Session["UserID"] != null)
            {
                string userid = Session["UserID"].ToString();
                companyid = new Catering_Rules.T_Personnel().GetCompanyIDByPID(Convert.ToInt32(userid));
            }
            string title = "";
            DateTime tempdate = DateTime.MinValue;
            DateTime.TryParse(Year + "-01-01 00:00:11", out tempdate);
            title = Bind_Data_to_Controls.Bind_ReportTitle(AreaID.ToString(), ClassID.ToString(), tempdate, "人均成本走势");
            DataTable dt = vm.GetRenJunChengBenZouShi(companyid, AreaID, ClassID, rawid, Year);
            Dictionary<string, Dictionary<int, decimal>> tempdic = InitDicModel(rawName);
            if (dt != null && dt.Rows.Count > 0)
            {
                for (int i = 0; i < dt.Rows.Count; i++)
                {
                    string RAWTName = dt.Rows[i]["RAWName"].ToString();
                    int tempmonth = Convert.ToInt32(dt.Rows[i]["MonthDay"]);
                    decimal tempnumber = Convert.ToDecimal(dt.Rows[i]["Number"]);
                    decimal tempmoney = 0;
                    if (!(dt.Rows[i]["AmountMoney"] is DBNull)) tempmoney = Convert.ToDecimal(dt.Rows[i]["AmountMoney"]);
                    if (tempdic.ContainsKey(RAWTName) && tempdic[RAWTName].ContainsKey(tempmonth) && tempnumber > 0)
                    {
                        tempdic[RAWTName][tempmonth] = vm.GetMathResult(tempmoney / tempnumber, 1, 3);
                    }
                }
                dt.Dispose();
            }
            string str = "";
            if (tempdic != null)
            {
                StringBuilder Sresult = new StringBuilder("");
                int n = 0;
                foreach (string key in tempdic.Keys)
                {
                    Sresult.Append("{");
                    Sresult.Append("name:'" + key + "',");
                    Sresult.Append("data: [");
                    string StrData = "";
                    for (int i = 0; i < 12; i++)
                    {
                        if (tempdic[key].ContainsKey(i + 1)) StrData += tempdic[key][i + 1] + ",";
                        else StrData += "0,";
                    }
                    StrData = StrData.Length > 0 ? StrData.Substring(0, StrData.Length - 1) : "";
                    Sresult.Append(StrData + "]");
                    Sresult.Append("},");
                    n++;
                }
                str = Sresult.ToString();
            }
            str = str.Length > 0 ? str.Substring(0, str.Length - 1) : "";
            ClientScript.RegisterStartupScript(ClientScript.GetType(), "myScript", "GetDataGroup('" + title + "'," + str + ");", true);
        }

        protected void CheckBoxList1_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (CheckBoxList1.SelectedItem.Text.Equals("全部"))
            {
                if (CheckBoxList1.SelectedItem.Selected)
                {
                    for (int i = 0; i < CheckBoxList1.Items.Count; i++)
                    {
                        CheckBoxList1.Items[i].Selected = true;
                    }
                }
            }
            else
            {
                int num = 0;
                for (int i = 1; i < CheckBoxList1.Items.Count; i++)
                {
                    if (CheckBoxList1.Items[i].Selected == true)
                    {
                        num = num + 1;
                    }
                }
                if (num == (CheckBoxList1.Items.Count - 1))
                {
                    for (int i = 0; i < CheckBoxList1.Items.Count; i++)
                    {
                        CheckBoxList1.Items[i].Selected = false;
                    }
                }
            }
        }

        /// <summary>
        /// 获取选中大类
        /// </summary>
        /// <returns></returns>
        public int[] GetRaw()
        {
            int[] RawId;//食品大类ID
            int selectNum1 = 0;
            for (int i = 0; i < CheckBoxList1.Items.Count; i++)
            {
                if (CheckBoxList1.Items[i].Selected)
                {
                    selectNum1 += 1;
                }
            }

            RawId = new int[selectNum1];
            int numTemp = 0;
            for (int i = 0; i < CheckBoxList1.Items.Count; i++)
            {
                if (CheckBoxList1.Items[i].Selected)
                {
                    RawId[numTemp] = Int32.Parse(CheckBoxList1.Items[i].Value);
                    numTemp += 1;
                }
            }
            return RawId;
        }

        public string[] GetRawName()
        {
            string[] RawName;//食品大类ID
            int selectNum1 = 0;
            for (int i = 0; i < CheckBoxList1.Items.Count; i++)
            {
                if (CheckBoxList1.Items[i].Selected)
                {
                    selectNum1 += 1;
                }
            }

            RawName = new string[selectNum1];
            int numTemp = 0;
            for (int i = 0; i < CheckBoxList1.Items.Count; i++)
            {
                if (CheckBoxList1.Items[i].Selected)
                {
                    RawName[numTemp] = CheckBoxList1.Items[i].Text;
                    numTemp += 1;
                }
            }
            return RawName;
        }

        private Dictionary<string, Dictionary<int, decimal>> InitDicModel(string[] RawName)
        {
            Dictionary<string, Dictionary<int, decimal>> tempdic = new Dictionary<string, Dictionary<int, decimal>>();
            for (int i = 0; i < RawName.Length; i++)
            {
                if (RawName[i] != "全部") tempdic.Add(RawName[i], InitSingleModel());
            }
            return tempdic;
        }

        private Dictionary<int, decimal> InitSingleModel()
        {
            Dictionary<int, decimal> temp1 = new Dictionary<int, decimal>();
            for (int i = 1; i < 13; i++)
            {
                temp1.Add(i, 0);
            }
            return temp1;
        }
    }
}